package homework4.action_e;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import homework4.Bean.Book;
import org.apache.struts2.ServletActionContext;

import java.util.ArrayList;

/**
 * @author CoderXshuai
 */
public class BookAction extends ActionSupport implements ModelDriven {
    private Book books = new Book();
    private ArrayList<Book> book_list;
    private String key;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public Object getModel() {
        // TODO Auto-generated method stub
        return books;
    }

    public String Add() {
        try {
            if (ServletActionContext.getServletContext().getAttribute("booklist") == null) {
                book_list = new ArrayList<Book>();
                books.setBookId(0);
                books.setFlag(1);
                book_list.add(0, books);
                ServletActionContext.getServletContext().setAttribute("booklist", book_list);
            } else {
                book_list = (ArrayList<Book>) ServletActionContext.getServletContext().getAttribute("booklist");
                books.setBookId(book_list.size());
                books.setFlag(1);
                book_list.add(book_list.size(), books);
                ServletActionContext.getServletContext().setAttribute("booklist", book_list);
            }
            return SUCCESS;
        } catch (Exception exp) {
            return ERROR;
        }

    }

    public String search() {
        book_list = (ArrayList<Book>) ServletActionContext.getServletContext().getAttribute("booklist");
        return SUCCESS;
    }

    public String Get() {
        try {
            String id_tmp = ServletActionContext.getRequest().getParameter("id");
            if (id_tmp != null && id_tmp != "") {
                int id = Integer.parseInt(id_tmp);
                if (ServletActionContext.getServletContext().getAttribute("booklist") != null) {
                    book_list = (ArrayList<Book>) ServletActionContext.getServletContext().getAttribute("booklist");
                    books = book_list.get(id);
                    return "editindex";
                } else
                    return ERROR;
            } else
                return ERROR;
        } catch (Exception exp) {
            return ERROR;
        }

    }

    public String Edit() {
        try {

            if (ServletActionContext.getServletContext().getAttribute("booklist") != null) {
                book_list = (ArrayList<Book>) ServletActionContext.getServletContext().getAttribute("booklist");
                books.setFlag(1);
                book_list.set(books.getBookId(), books);
                ServletActionContext.getServletContext().setAttribute("booklist", book_list);
                return SUCCESS;
            } else
                return ERROR;

        } catch (Exception exp) {
            return ERROR;
        }
    }

    public String Delete() {
        try {
            String id_tmp = ServletActionContext.getRequest().getParameter("id");
            if (id_tmp != null && id_tmp != "") {
                int id = Integer.parseInt(id_tmp);
                if (ServletActionContext.getServletContext().getAttribute("booklist") != null) {
                    book_list = (ArrayList<Book>) ServletActionContext.getServletContext().getAttribute("booklist");
                    books = book_list.get(id);
                    books.setFlag(0);
                    book_list.set(id, books);
                    ServletActionContext.getServletContext().setAttribute("booklist", book_list);
                    return SUCCESS;
                } else
                    return ERROR;
            } else
                return ERROR;
        } catch (Exception exp) {
            return ERROR;
        }
    }

    public Book getBooks() {
        return books;
    }

    public void setBooks(Book books) {
        this.books = books;
    }

    public ArrayList<Book> getBook_list() {
        return book_list;
    }

    public void setBook_list(ArrayList<Book> book_list) {
        this.book_list = book_list;
    }

    //ISBN验证
    private static boolean CheckISBN(String ISBN) {
        String[] strings = ISBN.split("");
        int result = 0;
        for (int i = 0; i < strings.length - 1; i++) {
            if (i % 2 == 0) {
                result = result + Integer.parseInt(strings[i]);
            } else {
                result = result + 3 * Integer.parseInt(strings[i]);
            }
        }
        int lastIndeed = Integer.parseInt(strings[strings.length - 1]);
        int last = (10 - result % 10);
        if ((last == 10 && lastIndeed == 0) || last == lastIndeed) {
            return true;
        } else {
            return false;
        }
    }

    public void validateAdd() {
        if (books.getISBN().length() != 13) {
            super.addFieldError("books.ISBN", "ISBN必须13位");
        }
        if (CheckISBN(books.getISBN())) {
            this.addFieldError("books.ISBN", "ISBN不符合规则");
        }
        if (!books.getPress().contains("出版社")) {
            this.addFieldError("books.press", "必须包含“出版社”一词");
        }
    }
    public void SaveImg(){

    }
}
